import json
# 这里以分词为例，其它算法的API名称和参数请参考文档
from aliyunsdkalinlp.request.v20200629 import GetNerChEcomRequest
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException


class DataProcess:
    def __init__(self):
        # 创建AcsClient实例
        self.client = AcsClient(
            "LTAI5tMi3dRiExmJf2QwG4uo",
            "BAZrjBbzPaSIyfEPTU0NFuOcJFfrfB",
            "cn-hangzhou"
        )

    ''' 商品名称抽象 '''

    def get_goods_name(self, str1):
        request = GetNerChEcomRequest.GetNerChEcomRequest()
        request.set_Text(str1)
        request.set_ServiceCode("alinlp")
        # request.set_TokenizerId("GENERAL_CHN")
        response = self.client.do_action_with_exception(request)
        resp_obj = json.loads(response)
        resp_data = resp_obj['Data']
        # print(resp_data)
        new_dict = json.loads(resp_data)
        # 商品名称抽象
        goods_name = ''
        for dict in new_dict['result']:
            if dict['tag'] == '品类':
                word = dict['word']
                if word not in goods_name:
                    goods_name += dict['word']
        # print(goods_name)
        return goods_name

    '''获取商品适用人群'''

    def get_crowd(self, str1):
        request = GetNerChEcomRequest.GetNerChEcomRequest()
        request.set_Text(str1)
        request.set_ServiceCode("alinlp")
        # request.set_TokenizerId("GENERAL_CHN")
        response = self.client.do_action_with_exception(request)
        resp_obj = json.loads(response)
        resp_data = resp_obj['Data']
        # 得到人群
        crowd = []
        new_dict = json.loads(resp_data)
        for dict in new_dict['result']:
            if dict['tag'] == '人群':
                crowd.append(dict['word'])
        # print(crowd)
        return crowd

    '''获取品牌同义词'''

    def get_brand_synonym(self, str2):
        request = GetNerChEcomRequest.GetNerChEcomRequest()
        request.set_Text(str2)
        request.set_ServiceCode("alinlp")
        # request.set_TokenizerId("GENERAL_CHN")
        response = self.client.do_action_with_exception(request)
        resp_obj = json.loads(response)
        resp_data = resp_obj['Data']
        # print(resp_data)
        new_dict = json.loads(resp_data)
        # 品牌名同义词
        brand_synonym = []
        brand_load = new_dict['result']
        for dict in brand_load:
            brandsy = dict['synonym']
            if brandsy:
                brand_synonym = brandsy.split(';')
        # print(brand_synonym)
        return brand_synonym

    '''获取场景、主题、分类的同义词'''

    def get_stc_synonym(self, str3):
        request = GetNerChEcomRequest.GetNerChEcomRequest()
        # request.set_Text("家用电器$生活电器")
        request.set_Text(str3)
        request.set_ServiceCode("alinlp")
        # request.set_TokenizerId("GENERAL_CHN")
        response = self.client.do_action_with_exception(request)
        resp_obj = json.loads(response)
        resp_data = resp_obj['Data']
        stc = str3.split('$')
        # 考虑到有的商品数据行 没有分类
        if len(stc) == 2:
            stc.append("")
        # print(stc[0], stc[1], stc[2])
        # 场景同义词
        scene_synonym = []
        # 主题同义词
        theme_synonym = []
        # 分类同义词
        category_synonym = []

        new_dict = json.loads(resp_data)
        stc_load = new_dict['result']
        for dict in stc_load:
            if dict['word'] in stc[0]:
                scenesy = dict['synonym']
                if scenesy:
                    scene_synonym = scenesy.split(";")
            if dict['word'] in stc[1]:
                themesy = dict['synonym']
                if themesy:
                    theme_synonym = themesy.split(";")
            if dict['word'] in stc[2]:
                categorysy = dict['synonym']
                if categorysy:
                    category_synonym = categorysy.split(";")
        # print(scene_synonym, theme_synonym, category_synonym)
        return scene_synonym, theme_synonym, category_synonym


if __name__ == '__main__':
    # “南极人加绒加厚儿童保暖内衣套装,南极人,母婴用品$童装$套装”
    print("测试： 南极人加绒加厚儿童保暖内衣套装,南极人,母婴用品$童装$套装 \n")

    handler = DataProcess()
    handler.get_goods_name("南极人加绒加厚儿童保暖内衣套装")
    handler.get_crowd("南极人加绒加厚儿童保暖内衣套装")
    handler.get_brand_synonym("南极人")
    handler.get_stc_synonym("箱包$女包$钱包卡包")
